<?php

class HargasewaController extends Zend_Controller_Action
{
    private $_hargasewa;
    public function init()
    {
        $this->view->site = $this->getRequest()->getControllerName();
        $this->_hargasewa = new Application_Model_HargaSewa();
        
    }

    public function indexAction()
    {
        $hargaSewa = $this->_hargasewa->getAllData();
        $this->view->hargaSewa = $hargaSewa;
        $row = $this->_hargasewa->getCount();
        if ($row == 0)
          $this->view->notFound = '<div class="notfound">Tidak Ada Data di Halaman Ini</div>';
        else
          $this->view->notFound = '<div class="notfound">Terdapat '.$row.' Data Pada Tabel Ini</div>';
    }

    public function addAction()
    {
        
        $form = new Application_Form_HargasewaForm();
        $form->removeDecorator('form');

        $submit = $form->getElement('submit');
        $batal = $form->getElement('batal');
        
        $form->removeElement('submit');
        $form->removeElement('batal');
                
        $this->view->form = $form;
        
        $this->view->submit = $submit;
        $this->view->batal = $batal;
        
        
        if($this->_request->isPost())
        {
            if($form->isValid($this->_request->getPost()))
            {   
                if($this->_hargasewa->checkHarga($form->getValue('daya_kva'))) {
                        $this->view->error = 'Data Sudah Ada<br />Edit Data yang Ada ?';
                } else {
                    $data = array(
                        'daya_kva' => $form->getValue('daya_kva'),
                        'harian' => $form->getValue('harian'),
                        'mingguan' => $form->getValue('mingguan'),
                        'bulanan' => $form->getValue('bulanan'),
                        'jenis_sewa' => 'Tanpa BBM'
                    );
                    
                    $this->_hargasewa->hargaSewaBaru($data);

                    $data = array(
                        'daya_kva' => $form->getValue('daya_kva'),
                        'harian' => $form->getValue('harian_bbm'),
                        'mingguan' => $form->getValue('mingguan_bbm'),
                        'bulanan' => $form->getValue('bulanan_bbm'),
                        'jenis_sewa' => 'Dengan BBM'
                    );

                    $this->_hargasewa->hargaSewaBaru($data);
                    $this->_redirect('hargasewa');
                 }
            } else {
                $form->populate($this->_request->getPost());
            }
        }
    }

    public function editAction()
        {   if($this->_getParam('id')) {
            $form = new Application_Form_HargasewaForm();
            $form->removeDecorator('form');

            $submit = $form->getElement('submit');
            $batal = $form->getElement('batal');

            $id = $form->getElement('id_harga_sewa');
            $id_bbm = $form->getElement('id_harga_sewa');

            $form->removeElement('submit');
            $form->removeElement('batal');

            $this->view->form = $form;

            $this->view->submit = $submit;
            $this->view->batal = $batal;
            $this->view->id = $id;
            $this->view->id_bbm = $id_bbm;

            $daya_kva = $this->_getParam('id');
            
            $tanpaBBM = $this->_hargasewa->getRow($daya_kva, 'Tanpa BBM');
            $denganBBM = $this->_hargasewa->getRow($daya_kva, 'Dengan BBM');

            if($this->_request->isPost())
            {
                $this->view->id_harga_sewa_bbm = $denganBBM['id_harga_sewa'];
                if($form->isValid($this->_request->getPost())){
                    $daya_kva = $form->getValue('daya_kva');
                    if($this->_hargasewa->checkHarga($daya_kva) and ($daya_kva != $tanpaBBM['daya_kva'])) {
                        $this->view->error = 'Data Sudah Ada<br />Edit Data yang Ada ?';
                    } else {
                        
                        
                        $data = array(
                                'daya_kva' => $form->getValue('daya_kva'),
                                'harian' => $form->getValue('harian'),
                                'mingguan' => $form->getValue('mingguan'),
                                'bulanan' => $form->getValue('bulanan'),
                            );
                            
                        $id_tanpa_bbm = $form->getValue('id_harga_sewa');
                        var_dump($data);
                        var_dump($id_tanpa_bbm);
                        $this->_hargasewa->hargaSewaUpdate($data, $id_tanpa_bbm);
                        
                        $data = array(
                                'daya_kva' => $form->getValue('daya_kva'),
                                'harian' => $form->getValue('harian_bbm'),
                                'mingguan' => $form->getValue('mingguan_bbm'),
                                'bulanan' => $form->getValue('bulanan_bbm'),
                        );
                        
                        $id_dengan_bbm = $denganBBM['id_harga_sewa'];;
                        var_dump($data);
                        var_dump($id_dengan_bbm);
                        $this->_hargasewa->hargaSewaUpdate($data, $id_dengan_bbm);
                        
                        $this->_redirect('hargasewa');
                    }
                } else {
                    $form->populate($this->_request->getPost());
                }
            } else {
                $form->populate($tanpaBBM);
                
                $data = array(
                'harian_bbm' => $denganBBM['harian'],
                'mingguan_bbm' => $denganBBM['mingguan'],
                'bulanan_bbm' => $denganBBM['bulanan']
                );
                
                $this->view->id_harga_sewa_bbm = $denganBBM['id_harga_sewa'];
                $form->populate($data);
            }
        } else {
            $this->_redirect('hargasewa');
        }
    }

    public function deleteAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $daya_kva = $this->getRequest()->getParam('id');
        if($daya_kva)
        {
            $this->_hargasewa->hargasewaDelete($daya_kva);
        } else {
            $this->_redirect('hargasewa');
        }
    }


}







